package com.cn.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.pojo.Dish;
import com.cn.response.Result;
import com.cn.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 功能描述: <br>
 *
 * @program: wx-nimi-bank
 * @description: 菜品控制层
 * @author: Mr.TYJ
 * @create: 2022-05-27 11:37
 **/
@RestController
@CrossOrigin
@Slf4j
@Api(tags = "菜品控制层")
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 当点击一级菜单的时候查询
     * @param current
     * @param size
     * @param name
     * @param status
     * @param pid
     * @return
     */
    @ApiModelProperty(value = "查询菜品",notes = "查询节点的所有菜品还有分类查询")
    @PostMapping("/selectAllByCate")
    public Result selectAllByCate(@RequestParam(value = "current",required = true,defaultValue ="1") Integer current,
                                  @RequestParam(value = "size",required = true,defaultValue ="3")Integer size,
                                  @RequestParam(value = "name",required = false) String name,
                                  @RequestParam(value = "status",required = false)Integer status,
                                  @RequestParam(value = "pid",required = false)Long pid,
    @RequestParam(value = "id",required = false)Long id){
        Page<Dish> page=new Page<>(current,size);
        Page<Dish> pageList=dishService.selectAllByPid(page,name,status,pid,id);
        return Result.ok().data("pageList",pageList);
    }

    /**
     * 暂抛弃
     * @param current
     * @param size
     * @param name
     * @param status
     * @param categoryId
     * @return
     */
    @ApiModelProperty(value = "查询菜品",notes = "查询节点的所有菜品还有分类查询")
    @PostMapping("/selectAllByDish")
    public Result selectAllByDish(@RequestParam(value = "current",required = true,defaultValue ="1") Integer current,
                                  @RequestParam(value = "size",required = true,defaultValue ="3")Integer size,
                                  @RequestParam(value = "name",required = false) String name,
                                  @RequestParam(value = "status",required = false)Integer status,
                                  @RequestParam(value = "categoryId",required = false)Long categoryId){
        Page<Dish> page=new Page<>(current,size);
        Page<Dish> pageList=dishService.selectAllByPid(page,name,status,categoryId,categoryId);
        return Result.ok().data("pageList",pageList);
    }

    @ApiModelProperty(value = "更新菜品", notes = "更新菜品")
    @PostMapping("/update")
    public Result getUpdateById(@RequestBody Dish dish) {
        dish.setUpdateTime(new Date());
        Integer num = dishService.getUpdate(dish);
        if (num > 0) {
            return Result.ok().data("code", 200);
        } else {
            return Result.ok().data("code", 0);
        }
    }
        @ApiModelProperty(value = "新增菜品",notes = "新增菜品")
        @PostMapping("/insert")
        public Result getInsert(@RequestBody Dish dish) {
            Integer num = dishService.getInsert(dish);
            if (num > 0) {
                return Result.ok().data("code", 200);
            } else {
                return Result.ok().data("code", 0);
            }

        }

     @ApiModelProperty(value = "删除",notes = "删除菜品")
     @PostMapping("/delete")
      public Result getDeleteById(@RequestParam(value = "id",required = true) Long id){
      Integer num=dishService.getDelete(id);
           if (num>0){
             return Result.ok().data("code",200);
           }else {
             return Result.ok().data("code",0);
        }

    }

    @ApiModelProperty(value = "修改状态", notes = "修改状态")
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam(value = "id",required = true) Long id,
                               @RequestParam(value = "status",required = true)Integer status) {
        Dish user = new Dish();
        user.setStatus(status);
        UpdateWrapper<Dish> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        boolean update = dishService.update(user, updateWrapper);
        if (update) {
            return Result.ok().message("修改状态成功");
        }
        return Result.error().message("修改状态失败真晦气");
    }

    @ApiModelProperty(value = "通过id查询", notes = "通过id查询")
    @PostMapping("selectById")
    public Result selectById(@RequestParam(value = "id",required = true) Long id) {
        Dish update = dishService.selectById(id);
        return Result.ok().data("Dish",update);
    }

}
